package com.jichangxiu.framework.config;

import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;

import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;

@Slf4j
@Configuration
@EnableTransactionManagement
public class AtomikosConfig {

    @Bean
    public TransactionManager transactionManager() {
        log.debug("【AtomikosConfig】加载【TransactionManager】配置");
        UserTransactionManager transactionManager = new UserTransactionManager();
        transactionManager.setForceShutdown(false);
        return transactionManager;
    }

    @Bean
    public UserTransaction userTransaction() throws Throwable {
        log.debug("【AtomikosConfig】加载【UserTransaction】配置");
        UserTransactionImp userTransaction = new UserTransactionImp();
        userTransaction.setTransactionTimeout(10000);
        return userTransaction;
    }

    @Bean
    public PlatformTransactionManager platformTransactionManager() throws Throwable {
        log.debug("【AtomikosConfig】加载【PlatformTransactionManager】配置");
        return new JtaTransactionManager(userTransaction(), transactionManager());
    }

}
